<template>
	<view>
		
	</view>
</template>

<script>
	var sign = require('../../commons/sign.js')
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			getUserInfo:function(res){
				console.log(res);
			}
		},
		onLoad:function(options){
			var _this = this;
			// 从服务器获取token
			var token = sign.sign(_this.apiServer);
			console.log(token);
			// app登录
			// #ifdef APP-PLUS
			uni.login({
				provider: 'weixin',
				// 登录成功
				success: (res) => {
					console.log("res:"+JSON.stringify(res));
					// 获取用户信息
					uni.getUserInfo({
						// 成功获取用户信息
						success:function(info){
							console.log("info"+info)
							// 去请求时带上token
							var sign = uni.getStorageSync('sign');
							uni.request({
								url: _this.apiServer+'login',
								method: 'POST',
								header: {'content-type' : "application/x-www-form-urlencoded"},
								data: {
									openid : info.userInfo.openId,
									name   : info.userInfo.nickName,
									face   : info.userInfo.avatarUrl,
									sign   : sign
								},
								success: (res) => {
									console.log("登录入库"+JSON.stringify(res));
									if(res.data.code == 200){
										uni.showToast({
											title: '登录成功'
										})
										uni.setStorageSync('SNAME', res.data.data.name + '');
										uni.setStorageSync('OPENID', res.data.data.openid + '');
										uni.setStorageSync('SFACE', res.data.data.face + '');
										// 登录成功后跳转到原来的页面
										if (options.backtype == 1) {
											uni.redirectTo({
												url: options.backpage
											});
										} else{
											uni.switchTab({
												url: options.backpage
											})
										}
									} else {
										uni.showToast({
											title: res.data
										})
									}
								}
							})
						},
						// 失败
						fail:function(){
							uni.showToast({
								title: "微信登录授权失败！",
								icon: "none"
							})
						}
					})
				},
				fail:function(){
					uni.showToast({
						title: "微信登录授权失败！",
						icon:"none"
					})
				}
			})
			// #endif
			
		}
	}
</script>

<style>

</style>
